package com.yft.service.purchasepos;

import com.yft.dao.base.BaseDaoUtils;
import com.yft.dao.purchasepos.PosPurchaseOrderHeaderDao;
import com.yft.entity.base.Order;
import com.yft.entity.base.Pager;
import com.yft.entity.posinformation.PosMachine;
import com.yft.entity.posinformation.PosModel;
import com.yft.entity.purchasepos.PosPurchaseOrderDetail;
import com.yft.entity.purchasepos.PosPurchaseOrderHeader;
import com.yft.enumeration.PosStatus;
import com.yft.service.pos_information.PosMachineService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("posPurchaseOrderHeaderService")
public class PosPurchaseOrderHeaderServiceImp implements PosPurchaseOrderHeaderService {
	Logger log = Logger.getLogger(PosPurchaseOrderHeaderServiceImp.class);
	
	@Autowired(required = true)
	private PosPurchaseOrderHeaderDao posPurchaseOrderHeaderDao;

    @Autowired(required = true)
    private PosPurchaseOrderDetailService posPurchaseOrderDetailService;

    @Autowired(required = true)
    private BaseDaoUtils baseDaoUtils;

    @Autowired(required = true)
    private PosMachineService posMachineService;

    @Override
    public Object addPosPurchaseOrderHeader(PosPurchaseOrderHeader posPurchaseOrderHeader) {
        return posPurchaseOrderHeaderDao.addPosPurchaseOrderHeader(posPurchaseOrderHeader);
    }

    @Override
    public PosPurchaseOrderHeader getPosPurchaseOrderHeader(String posPurchaseOrderHeaderPk) {
        return posPurchaseOrderHeaderDao.getPosPurchaseOrderHeader(posPurchaseOrderHeaderPk);
    }

    @Override
    public boolean updateValues(PosPurchaseOrderHeader condition, String cumstomStr) {
        return posPurchaseOrderHeaderDao.updateValues(condition,cumstomStr);
    }

    @Override
    public List<PosPurchaseOrderHeader> getPosPurchaseOrderHeaderList(PosPurchaseOrderHeader condition, Order order, Pager pager) {
        return posPurchaseOrderHeaderDao.getPosPurchaseOrderHeaderList(condition,order,pager);
    }

    @Transactional
    public boolean addPosPurchaseOrder(PosPurchaseOrderHeader posPurchaseOrderHeader, List<PosPurchaseOrderDetail> itemList) {
        if(posPurchaseOrderHeader == null || itemList == null || itemList.isEmpty()) return false;
        String serialNum = posPurchaseOrderHeaderDao.makePurchaseHeaderSerialNum();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        posPurchaseOrderHeader.setPurchaseOrderNo("Po" + dateFormat.format(new Date()) + serialNum);
        Object headerId = this.addPosPurchaseOrderHeader(posPurchaseOrderHeader);
        if(headerId == null) return false;
        String headerIdStr = (String)headerId;
        PosPurchaseOrderHeader con = new PosPurchaseOrderHeader();
        con.setHeaderId(headerIdStr);
        for(PosPurchaseOrderDetail tmp : itemList){
            tmp.setPosPurchaseOrderHeader(con);
            tmp.setHeaderId(headerIdStr);
        }
        return posPurchaseOrderDetailService.saveAllPosPurchaseOrderDetails(itemList);
    }

    @Transactional
    public boolean updatePosPurchaseOrder(PosPurchaseOrderHeader posPurchaseOrderHeader, List<PosPurchaseOrderDetail> waitAddItems, List<PosPurchaseOrderDetail> waitDelItems) {
        if(posPurchaseOrderHeader!=null)
            posPurchaseOrderHeaderDao.updateValues(posPurchaseOrderHeader,null);
        if(waitAddItems != null){
            posPurchaseOrderDetailService.saveAllPosPurchaseOrderDetails(waitAddItems);
        }
        if(waitDelItems != null){
            for(PosPurchaseOrderDetail detail : waitDelItems){
                posPurchaseOrderDetailService.delPosPurchaseOrderDetail(detail);
            }
        }
        return true;
    }

    @Transactional
    public boolean cancelPosPurchase(String headerId) {
        if(headerId!=null && headerId.length()>0){
            PosPurchaseOrderHeader header = new PosPurchaseOrderHeader();
            header.setHeaderId(headerId);
            int count = baseDaoUtils.updateHql("update PosPurchaseOrderHeader set posPurchaseOrderStatus = 'CANCEL' where headerId=:headerId and posPurchaseOrderStatus = 'NEW'",header);
            if(count==1){
                return true;
            }
        }
        return false;
    }

    @Transactional
    public boolean auditPosPurchaseOrder(String headerId,String channelName) {
        if(headerId!=null && headerId.length()>0){
            PosPurchaseOrderDetail detail = new PosPurchaseOrderDetail();
            detail.setHeaderId(headerId);
            List<PosPurchaseOrderDetail> posPurchaseOrderDetailList = posPurchaseOrderDetailService.getPosPurchaseOrderDetailList(detail,null,null);
            if(posPurchaseOrderDetailList==null || posPurchaseOrderDetailList.isEmpty()) return false;
            PosPurchaseOrderDetail handleDetail = null;
            List<PosMachine> posMachineList = new ArrayList<PosMachine>();
            int j =  posPurchaseOrderDetailList.size();
            for(int i = 0;i<j;i++){
                handleDetail = posPurchaseOrderDetailList.get(i);
                if(handleDetail != null){
                    PosMachine posMachine = new PosMachine();
                    posMachine.setPutInTime(new Date());
                    posMachine.setMachineSnCode(handleDetail.getMachineSnCode());
                    posMachine.setTerminalSn(handleDetail.getTerminalSn());
                    posMachine.setPasswordKeySn(handleDetail.getPasswordKeySn());
                    posMachine.setPosModelId(handleDetail.getPosModelId());
                    posMachine.setPosModel(new PosModel(handleDetail.getPosModelId()));
                    posMachine.setPattern(handleDetail.getPattern());
                    posMachine.setModelName(handleDetail.getModelName());
                    posMachine.setChannelName(channelName);
                    posMachine.setIsRecycle(0);
                    posMachine.setPosStatus(PosStatus.AVAILABLE);
                    posMachineList.add(posMachine);
                }
            }
            if(j!=posMachineList.size()) return false;
            boolean flag = baseDaoUtils.saveAllEntity(posMachineList);
            if(flag){
                PosPurchaseOrderHeader header = new PosPurchaseOrderHeader();
                header.setHeaderId(headerId);
                int count = baseDaoUtils.updateHql("update PosPurchaseOrderHeader set posPurchaseOrderStatus = 'SUCCESS' where headerId=:headerId and posPurchaseOrderStatus = 'NEW'",header);
                if(count==1){
                    return true;
                }
            }
        }
        return false;
    }
}
